package org.beetl.json.node;

import java.io.IOException;
import java.util.List;
import java.util.TreeSet;

import org.beetl.json.ActionComparator;
import org.beetl.json.ActionReturn;
import org.beetl.json.JsonWriter;
import org.beetl.json.Location;
import org.beetl.json.LocationAction;
import org.beetl.json.OutputNode;
import org.beetl.json.action.IInstanceAction;
import org.beetl.json.action.IValueAction;
import org.beetl.json.action.KeyIgnoreAction;
import org.beetl.json.loc.ThisLocation;

public class JavaObjectNode extends OutputNode {


	Class c;
	


	public JavaObjectNode(Class c) {
		this.c = c;
	}

	@Override
	public void render(Object obj, JsonWriter w) throws IOException {
		
		
		if (this.valueActions.size() != 0) {
			for (IValueAction valueAction : this.valueActions) {
				ActionReturn ar = valueAction.doit(obj, this,w);
				obj = ar.value;
				if (ar.process == ActionReturn.BREAK) {
					break;
				} else if (ar.process == ActionReturn.RETURN) {
					return;
				}
			}

		}

		w.writeValue(obj);

	}

	@Override
	public void setActionByLocation(List<Location> list) {
		for (Location l : list) {
			LocationAction action = l.getAction();
			if (action instanceof IValueAction) {
				if(action instanceof KeyIgnoreAction){
					this.ignore = true;
				}else if (isMatch(l)) {
					this.valueActions.add((IValueAction) action);
				}
			}
			
			
		}

	}

	protected boolean isMatch(Location location) {
		return location.match(this,this.c, null);
	}

}
